﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Binding.cs" company="">
//   
// </copyright>
// <summary>
//   The binding.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Bindings
{
    using System.Collections.Generic;

    /// <summary>
    /// The binding.
    /// </summary>
    /// <typeparam name="T1">
    /// </typeparam>
    /// <typeparam name="T2">
    /// </typeparam>
    public abstract class Binding<T1, T2> : IBinding<T1, T2>
    {
        #region Fields

        /// <summary>
        ///   The _bindings.
        /// </summary>
        private readonly List<IBinding> _bindings = new List<IBinding>();

        /// <summary>
        ///   The _service.
        /// </summary>
        private readonly IBindService _service;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Binding{T1,T2}"/> class.
        /// </summary>
        /// <param name="service">
        /// The service. 
        /// </param>
        /// <param name="obj1">
        /// The obj 1. 
        /// </param>
        /// <param name="obj2">
        /// The obj 2. 
        /// </param>
        protected Binding(IBindService service, T1 obj1, T2 obj2)
        {
            this._service = service;
            this.Object1 = obj1;
            this.Object2 = obj2;
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets the object 1.
        /// </summary>
        public T1 Object1 { get; private set; }

        /// <summary>
        ///   Gets the object 2.
        /// </summary>
        public T2 Object2 { get; private set; }

        #endregion

        #region Explicit Interface Properties

        /// <summary>
        ///   Gets the object 1.
        /// </summary>
        object IBinding.Object1
        {
            get
            {
                return this.Object1;
            }
        }

        /// <summary>
        ///   Gets the object 2.
        /// </summary>
        object IBinding.Object2
        {
            get
            {
                return this.Object2;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///   The bind.
        /// </summary>
        public virtual void Bind()
        {
            this._bindings.Clear();
        }

        /// <summary>
        ///   The unbind.
        /// </summary>
        public virtual void Unbind()
        {
            foreach (var binding in this._bindings)
            {
                binding.Unbind();
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The bind.
        /// </summary>
        /// <param name="obj1">
        /// The obj 1. 
        /// </param>
        /// <param name="obj2">
        /// The obj 2. 
        /// </param>
        /// <returns>
        /// The <see cref="IBinding"/> . 
        /// </returns>
        protected IBinding Bind(object obj1, object obj2)
        {
            return this._service.Bind(obj1, obj2);
        }

        /// <summary>
        /// The unbind.
        /// </summary>
        /// <param name="obj1">
        /// The obj 1. 
        /// </param>
        /// <param name="obj2">
        /// The obj 2. 
        /// </param>
        protected void Unbind(object obj1, object obj2)
        {
            this._service.Unbind(obj1, obj2);
        }

        /// <summary>
        /// The unbind.
        /// </summary>
        /// <param name="obj">
        /// The obj. 
        /// </param>
        protected void Unbind(object obj)
        {
            this._service.Unbind(obj);
        }

        #endregion
    }
}